/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * telaAlterarUsuario.java
 *
 * Created on 25/11/2012, 21:51:27
 */
package gui;

import Fachada.Fachada;
import classes.Usuario;
import erro.ConexaoException;
import erro.ConsultaException;
import erro.GeralException;
import javax.swing.JOptionPane;

/**
 *
 * @author George
 */
public class telaAlterarUsuario extends javax.swing.JFrame {

    private static Fachada f;
    String cpf;
    String fone;
    Integer codpf;

    /** Creates new form telaAlterarUsuario */
    public telaAlterarUsuario() {
        initComponents();
        this.setLocationRelativeTo(null);


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        RadioButtonUsu = new javax.swing.JRadioButton();
        RadioButtonAdm = new javax.swing.JRadioButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtBoxFoneUsu = new javax.swing.JTextField();
        txtBoxNomeUsu = new javax.swing.JTextField();
        txtBoxCPFUsu = new javax.swing.JTextField();
        try{
            javax.swing.text.MaskFormatter cpf= new javax.swing.text.MaskFormatter("###.###.###-##");
            txtBoxCPFUsu = new javax.swing.JFormattedTextField(cpf);
        }
        catch (Exception e){
        }
        jSeparator1 = new javax.swing.JSeparator();
        jLabel7 = new javax.swing.JLabel();
        txtBoxSenhaUsu = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        txtBoxLoginUsu = new javax.swing.JTextField();
        ButtonCancelarAltUsu = new javax.swing.JButton();
        ButtonSalvarAltUsu = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        LabelCodUsu = new javax.swing.JLabel();
        jButtonDeletarUsu = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        buttonGroup1.add(RadioButtonUsu);
        RadioButtonUsu.setFont(new java.awt.Font("Tahoma", 1, 12));
        RadioButtonUsu.setText("Usuário");

        buttonGroup1.add(RadioButtonAdm);
        RadioButtonAdm.setFont(new java.awt.Font("Tahoma", 1, 12));
        RadioButtonAdm.setText("Administrador");
        RadioButtonAdm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RadioButtonAdmActionPerformed(evt);
            }
        });

        jLabel4.setText("Login:");

        jLabel5.setText("Senha:");

        jLabel2.setText("CPF:");

        jLabel3.setText("Fone:");

        try{ 
            javax.swing.text.MaskFormatter tel= new javax.swing.text.MaskFormatter("(##)####-####"); 
            txtBoxFoneUsu = new javax.swing.JFormattedTextField(tel); 
        } 
        catch (Exception e){ 
        }

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        jLabel7.setText("Alterar Usuário");

        jLabel1.setText("Nome Completo:");

        ButtonCancelarAltUsu.setText("CANCELAR");
        ButtonCancelarAltUsu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonCancelarAltUsuActionPerformed(evt);
            }
        });

        ButtonSalvarAltUsu.setText("SALVAR");
        ButtonSalvarAltUsu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonSalvarAltUsuActionPerformed(evt);
            }
        });

        jLabel6.setText("Cod. Usuario:");

        jButtonDeletarUsu.setText("DELETAR");
        jButtonDeletarUsu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeletarUsuActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(ButtonSalvarAltUsu, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(ButtonCancelarAltUsu)
                                    .addGap(18, 18, 18)
                                    .addComponent(jButtonDeletarUsu, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(RadioButtonAdm)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(RadioButtonUsu)
                                    .addGap(19, 19, 19)))
                            .addComponent(LabelCodUsu)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtBoxFoneUsu, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtBoxCPFUsu, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtBoxSenhaUsu, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtBoxLoginUsu, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE))
                            .addComponent(txtBoxNomeUsu, javax.swing.GroupLayout.PREFERRED_SIZE, 286, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(74, 74, 74)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(24, 24, 24)
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(28, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(9, 9, 9)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(LabelCodUsu))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtBoxNomeUsu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtBoxCPFUsu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtBoxFoneUsu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtBoxLoginUsu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtBoxSenhaUsu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(RadioButtonUsu)
                    .addComponent(RadioButtonAdm))
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ButtonSalvarAltUsu, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ButtonCancelarAltUsu, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonDeletarUsu, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void RadioButtonAdmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RadioButtonAdmActionPerformed
        // TODO add your handling code here:}//GEN-LAST:event_RadioButtonAdmActionPerformed
    }
        private void ButtonSalvarAltUsuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonSalvarAltUsuActionPerformed
            telaAlterarUsuario tlaltusu = new telaAlterarUsuario();
            try {
                f = Fachada.getInstancia();
                Usuario usu = new Usuario();
                cpf = txtBoxCPFUsu.getText().replace(".", "");
                cpf = cpf.replace("-", "");
                fone = txtBoxFoneUsu.getText().replace("(", "");
                fone = fone.replace(")", "");
                fone = fone.replace("-", "");
                usu.setCodUsuario(Integer.parseInt(LabelCodUsu.getText()));
                usu.setNomeUsuario(txtBoxNomeUsu.getText());
                txtBoxNomeUsu.requestFocus();
                usu.setCpfUsuario(cpf);
                usu.setFoneUsuario(fone);
                usu.setLoginUsuario(txtBoxLoginUsu.getText());
                usu.setSenhaUsuario(txtBoxSenhaUsu.getText());
                if (RadioButtonAdm.isSelected()) {
                    codpf = 1;
                } else {
                    if (RadioButtonUsu.isSelected()) {
                        codpf = 2;
                    }
                }
                usu.setCodPerfil(codpf);
                try {
                    f.alterarUsu(usu);
                    telaAlterarUsuario.this.setVisible(false);
                    tlaltusu.dispose();
                } catch (ConsultaException ex) {
                    JOptionPane.showMessageDialog(null, ex.getMessage());
                } catch (ConexaoException ex) {
                    JOptionPane.showMessageDialog(null, ex.getMessage());
                }
            } catch (GeralException e) {
                JOptionPane.showMessageDialog(null, e.getMessage());
            }
    }//GEN-LAST:event_ButtonSalvarAltUsuActionPerformed

    private void ButtonCancelarAltUsuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonCancelarAltUsuActionPerformed
        telaAlterarUsuario tlaltusu = new telaAlterarUsuario();
        int i = JOptionPane.showConfirmDialog(null,
                "Deseja realmente Cancelar?", "Cancelar", JOptionPane.YES_NO_OPTION);
        if (i == JOptionPane.YES_OPTION) {
            telaAlterarUsuario.this.setVisible(false);
            tlaltusu.dispose();
        } else {
            repaint();
        }
    }//GEN-LAST:event_ButtonCancelarAltUsuActionPerformed

    private void jButtonDeletarUsuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeletarUsuActionPerformed
         int i = JOptionPane.showConfirmDialog(null,
                "Deseja realmente Deletar?", "Deletar", JOptionPane.YES_NO_OPTION);
        if (i == JOptionPane.YES_OPTION) {
            telaAlterarUsuario tlaltusu = new telaAlterarUsuario();
            try {
                f = Fachada.getInstancia();
                Usuario usu = new Usuario();
                usu.setCodUsuario(Integer.parseInt(LabelCodUsu.getText()));
                f.deletarUsu(usu);
                telaAlterarUsuario.this.setVisible(false);
                tlaltusu.dispose();
            } catch (ConsultaException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            } catch (ConexaoException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            } catch (GeralException e) {
                JOptionPane.showMessageDialog(null, e.getMessage());
            }
        } else {
            repaint();
        }
    }//GEN-LAST:event_jButtonDeletarUsuActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(telaAlterarUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(telaAlterarUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(telaAlterarUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(telaAlterarUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new telaAlterarUsuario().setVisible(true);

            }
        });
    }

    public void selecionado() {
        if (codpf == 1) {
            RadioButtonAdm.setSelected(true);
        } else {
            if (codpf == 2) {
                RadioButtonUsu.setSelected(true);
            }
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ButtonCancelarAltUsu;
    private javax.swing.JButton ButtonSalvarAltUsu;
    public javax.swing.JLabel LabelCodUsu;
    public javax.swing.JRadioButton RadioButtonAdm;
    public javax.swing.JRadioButton RadioButtonUsu;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButtonDeletarUsu;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JSeparator jSeparator1;
    public javax.swing.JTextField txtBoxCPFUsu;
    public javax.swing.JTextField txtBoxFoneUsu;
    public javax.swing.JTextField txtBoxLoginUsu;
    public javax.swing.JTextField txtBoxNomeUsu;
    public javax.swing.JTextField txtBoxSenhaUsu;
    // End of variables declaration//GEN-END:variables
}